$(function () {
    loadTree();
});

function loadTree() {
    $.ajax({
        type: "GET",
        url: "/api/post/count",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (data) {

            layui.use('laypage', function () {
                var laypage = layui.laypage;
                //执行一个laypage实例
                laypage.render({
                    elem: 'loadPage', //注意，这里的 loadPage 是 ID，不用加 # 号
                    count: data, //数据总数，从服务端得到
                    curr: 1,
                    limit: 10,//每页条数
                    jump: function (obj, first) {
                        //obj包含了当前分页的所有参数，比如：
                        //得到当前页，以便向服务端请求对应页的数据。console.log(obj.curr);
                        //得到每页显示的条数 console.log(obj.limit);
                        $.ajax({
                            type: "GET",
                            url: "/api/post/showTree?page=" + obj.curr + '&limit=' + obj.limit,
                            dataType: "json",
                            contentType: "application/json; charset=utf-8",
                            success: function (data) {
                                var ipg = data.data;
                                var records = '';
                                $("#treeRecords").empty();
                                if (data.code === "200") {
                                    for (var i = 0; i < ipg.records.length; i++) {
                                        records =
                                            '' + fmtYear(ipg.records[i].postDate) + '' +
                                            ' <div class="timeline-item">' +
                                            ' <time>' + fmt(ipg.records[i].postDate) + '</time>' +
                                            ' <a  href="/web/single.html?postId=' + ipg.records[i].postId + '&page=1&limit=5">' + ipg.records[i].postTitle + '</a>' +
                                            ' </div>';
                                        // if(ipg.records[i].postDate.getFullYear()!=){
                                        //
                                        // }
                                        $("#treeRecords").append(records);
                                    }
                                } else {
                                    //layer.msg(data.msg, {time: 1 * 1000});
                                }
                            }
                        });

                        //首次不执行
                        if (!first) {
                            //do something
                        }
                    }
                });
            });
        }
    });


}

//将时间戳转化为时间
function fmt(timestamp) {
//13位时间戳转换
    var date = new Date(timestamp / 1);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return m + '-' + d;
}

function fmtYear(timestamp) {
    //13位时间戳转换
    var date = new Date(timestamp / 1);
    var y = date.getFullYear();
    return y;
}